/*
 * Copyright 2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Reading from PE file as if it is loaded to memory by linker.
 */

#ifndef RVA_H_
#define RVA_H_

#include <windows.h>
/*
 * PE file mapped to memory and all information about sections that is needed
 * to map RVA addresses.
 */
typedef struct {
	PIMAGE_SECTION_HEADER sections;
	int numberOfSections;
	char* mapping;
	int fileAlignment;
	int sectionAlignment;
} pe_sections;

typedef struct {
	int rva;
	void* data;
	/* Start address of the current section */
	int sectStart;
	/* End address of the current section */
	int sectEnd;
	/*
	 * End address in the current section that is mapped to the file.
	 * Everything after that is zeros.
	 */
	int fileEnd;
	pe_sections* peSections;
} rva_pointer;

/* Initialize RVA pointer */
BOOL InitRVAPointer(rva_pointer* p, pe_sections* peSections, int rva);
BOOL SetRVA(rva_pointer* p, int rva);
DWORD ReadByte(rva_pointer* p);
BOOL ReadDWORD(rva_pointer* p, DWORD* data);
char* ReadString(rva_pointer* p);
BOOL ReadData(rva_pointer* p, void* data, int size);

#endif /* RVA_H_ */
